package com.tang.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tang.reggie.common.R;
import com.tang.reggie.pojo.Category;
import com.tang.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R page(int page,int pageSize){
        log.info("分类模块：进入分页功能：page= {},pageSize= {}",page,pageSize);
        IPage<Category> iPage=categoryService.findPage(page,pageSize);
        Category category = iPage.getRecords().get(1);
/*        //时间的格式是yyyy-MM-ddTHH:mm:ss
        System.out.println("你好："+category.getCreateTime());//你好：2021-05-27T09:16:58*/
        return R.success(iPage);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category){
        log.info("分类模块：进入添加功能：{}"+category.toString());
        categoryService.save(category);
        return R.success("分类添加成功");
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(Long id){
        log.info("分类模块：进入删除功能：id= {}"+id);
        categoryService.delete(id);
        return R.success("分类删除成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){
        log.info("分类模块：进入修改功能： {}",category);
        categoryService.updateById(category);
        return R.success("分类修改成功");
    }

    /**
     * 根据分类的类型type查询分类表
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R list(Category category){
        log.info("分类模块：进入菜品或套餐查询功能： category={}",category);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //排序条件
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        return R.success(list);
    }

}
